﻿using System;
using Csla.Xaml;
using Accounting.Library;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;
using System.Windows;

namespace csla4Skeleton.UI.SL.NghiepVu.Kho.K_PhieuXuatKho
{
    public class K_PhieuXuatKho_ViewModel : ViewModel<Accounting.Library.K_PhieuXuatKho>
    {
        #region Event Declare
        public delegate void ChildList_AddedNew_EventHandler(object sender, ChildListAddedNewArgs e);       
        public delegate void HangHoaIdChanged_EventHandler(object sender, HangHoaIdChangedEventArgs e);       
        public delegate void DoiTuongTHCPIDChanged_EventHandler(object sender, DoiTuongTHCPIDChangedEventArgs e);

        public event EventHandler LoadCompleted;
        public event EventHandler Saved;
        public event EventHandler Canceled;
        public event ChildList_AddedNew_EventHandler ChildList_AddedNew;
        public event HangHoaIdChanged_EventHandler hangHoaIdChanged;       
        public event DoiTuongTHCPIDChanged_EventHandler DoiTuongTHCPIDChanged;
        #endregion

        #region Refresh Model
        public K_PhieuXuatKho_ViewModel(string mode)
        {
            if (mode.Equals(BusinessConfiguration.Configuration.ChildWindow))
            {
                this.BeginRefresh("NewK_PhieuXuatKho");
            }           
        }

        public K_PhieuXuatKho_ViewModel()
        {
            BeginRefresh("NewK_PhieuXuatKho");            
        }

        public K_PhieuXuatKho_ViewModel(int Khoa)
        {
            BeginRefresh("GetK_PhieuXuatKho", Khoa);           
        }

        public void NewK_PhieuXuatKhoViewModel()
        {
            BeginRefresh("NewK_PhieuXuatKho");            
        }

        public void GetK_PhieuNhapKhoViewModel(int Khoa)
        {
            BeginRefresh("GetK_PhieuXuatKho", Khoa);
        }

        protected override void OnSaved()
        {
            base.OnSaved();
            if (Saved != null)
            {
                Saved(this, EventArgs.Empty);
            }

            if (Model != null)
            {
                InitinalizeEventHandler();
            }
        }

        protected override void DoCancel()
        {
            base.DoCancel();
            if (Canceled != null)
            {
                Canceled(this, EventArgs.Empty);
            }
        }

        public void SaveAndNew()
        {
            BeginSave();            
            NewK_PhieuXuatKhoViewModel();
        }

        public void SavePhieuXuatKho()
        {
            BeginSave();
        }

        protected override void OnRefreshed()
        {
            base.OnRefreshed();
            if (Model != null)
            {
                InitinalizeEventHandler();

                if (LoadCompleted != null)
                {
                    LoadCompleted(this, new EventArgs());
                }
            }
        }

        private void InitinalizeEventHandler() 
        {
            Model.K_PhieuXuatKho_ChiTiets.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<K_PhieuXuatKho_ChiTietChild>>(K_PhieuXuatKho_ChiTiets_AddedNew);
            Model.HangHoaIdChanged += new EventHandler<HangHoaIdChangedEventArgs>(OnHangHoaIdChanged);
        }
        #endregion
        
        #region Add K_PhieuXuatKho Details Item
        public void AddK_PhieuXuatKho_ChiTietItem()
        {
            Model.K_PhieuXuatKho_ChiTiets.AddNewItem(Model.Khoa);
        }

        void K_PhieuXuatKho_ChiTiets_AddedNew(object sender, Csla.Core.AddedNewEventArgs<K_PhieuXuatKho_ChiTietChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("K_PhieuXuatKho_ChiTiets"));
        }      

        protected virtual void OnChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (ChildList_AddedNew != null)
            {
                //Invokes the delegates.
                ChildList_AddedNew(sender, e);
            }
        }
      
        public void OnHangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
            if (hangHoaIdChanged != null)
            {
                hangHoaIdChanged(sender, e);
            }
        }

        public void OnDoiTuongTHCPIDChanged(object sender, DoiTuongTHCPIDChangedEventArgs e)
        {
            if (DoiTuongTHCPIDChanged != null)
            {
                DoiTuongTHCPIDChanged(sender, e);
            }
        }
        #endregion

        #region Delete K_PhieuXuatKho Details Item
        public void DeleteK_PhieuXuatKho_ChiTietItem(int Index)
        {
            Model.K_PhieuXuatKho_ChiTiets.DeleteItem(Index);
        }
        #endregion
    }
}
